package com.bnyy.medicalHousekeeper;

import android.content.Context;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;

import com.blankj.utilcode.util.ScreenUtils;

import java.util.function.Consumer;

public class DialogHelper {

    public static void showEditDialog(Context context, Consumer<String> consumer) {

    }

    public static void showNormalDialog(Context context, CharSequence content, OnButtonClickListener l) {
        showNormalDialog(context, "", content, "", "", l);
    }

    public static void showNormalDialog(Context context, String title, CharSequence content, String rStr, OnButtonClickListener l) {
        showNormalDialog(context, title, content, "", rStr, l);
    }

    public static void showNormalDialog(Context context, CharSequence content, String rStr, OnButtonClickListener l) {
        showNormalDialog(context, "", content, "", rStr, l);
    }

    public static void showNormalDialog(Context context, String title, CharSequence content, String lStr, String rStr, OnButtonClickListener l) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.dialog_normal, null);
        TextView tvTitle = view.findViewById(R.id.tv_title);
        TextView tvContent = view.findViewById(R.id.tv_content);
        TextView tvLeft = view.findViewById(R.id.tv_left);
        TextView tvRight = view.findViewById(R.id.tv_right);

        if (!TextUtils.isEmpty(lStr)) {
            tvLeft.setText(lStr);
        }
        if (!TextUtils.isEmpty(rStr)) {
            tvRight.setText(rStr);
        }
        if (!TextUtils.isEmpty(title)) {
            tvTitle.setText(title);
            tvContent.setText(Html.fromHtml(""));
        }

        tvContent.setText(content);

        AlertDialog alertDialog = builder.setCancelable(false)
                .setView(view)
                .create();
        view.findViewById(R.id.iv_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
                if (l != null) {
                    l.onCancelClick(alertDialog);
                }
            }
        });
        tvLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
                if (l != null) {
                    l.onLeftClick(alertDialog);
                }
            }
        });
        tvRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
                if (l != null) {
                    l.onRightClick(alertDialog);
                }
            }
        });
        alertDialog.show();

        Window window = alertDialog.getWindow();
        window.setBackgroundDrawableResource(android.R.color.transparent);
        WindowManager.LayoutParams attributes = window.getAttributes();
        attributes.width = ScreenUtils.getScreenWidth() / 5 * 4;
        window.setAttributes(attributes);
    }

    public static AlertDialog getTipsDialog(Context context, CharSequence message) {
        return getTipsDialog(context, message, null);
    }

    public static AlertDialog getTipsDialog(Context context, CharSequence message, View.OnClickListener l) {
        return showTipsDialog(context, "", message, "", 0, l);
    }

    public static void showTipsDialog(Context context, CharSequence message) {
        showTipsDialog(context, "", message, null);
    }

    public static void showTipsDialog(Context context, CharSequence title, CharSequence message) {
        showTipsDialog(context, title, message, null);
    }

    public static void showTipsDialog(Context context, CharSequence message, int buttonDrawable) {
        showTipsDialog(context, "", message, "", buttonDrawable, null);
    }

    public static void showTipsDialog(Context context, CharSequence message, View.OnClickListener l) {
        showTipsDialog(context, "", message, l);
    }

    public static void showTipsDialog(Context context, CharSequence title, CharSequence content, View.OnClickListener l) {
        showTipsDialog(context, title, content, "", 0, l);
    }

    public static void showTipsDialog(Context context, CharSequence title, CharSequence content, String buttonStr, View.OnClickListener l) {
        showTipsDialog(context, title, content, buttonStr, 0, l);
    }

    public static AlertDialog showTipsDialog(Context context, CharSequence title, CharSequence content, String buttonStr, int buttonDrawable, View.OnClickListener l) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.dialog_tips, null);

        AlertDialog alertDialog = builder.setCancelable(false)
                .setView(view)
                .create();

        view.findViewById(R.id.iv_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
            }
        });
        TextView tvTitle = view.findViewById(R.id.tv_title);
        TextView tvMessage = view.findViewById(R.id.tv_message);
        if (!TextUtils.isEmpty(title)) {
            tvTitle.setText(title);
        }
        tvMessage.setText(content);
        TextView tvConfirm = view.findViewById(R.id.tv_confirm);
        if (!TextUtils.isEmpty(buttonStr)) {
            tvConfirm.setText(buttonStr);
        }
        if (buttonDrawable != 0) {
            tvConfirm.setBackground(context.getDrawable(buttonDrawable));
        }
        tvConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
                if (l != null) {
                    l.onClick(v);
                }
            }
        });
        alertDialog.show();

        Window window = alertDialog.getWindow();
        window.setBackgroundDrawableResource(android.R.color.transparent);
        WindowManager.LayoutParams attributes = window.getAttributes();
        attributes.width = ScreenUtils.getScreenWidth() / 5 * 4;
        window.setAttributes(attributes);

        return alertDialog;
    }

    public static abstract class Callback implements OnButtonClickListener {
        public void onCancelClick(AlertDialog alertDialog) {

        }

        @Override
        public void onLeftClick(AlertDialog alertDialog) {

        }

        @Override
        public void onRightClick(AlertDialog alertDialog) {

        }
    }

    public interface OnButtonClickListener {
        void onCancelClick(AlertDialog alertDialog);

        void onLeftClick(AlertDialog alertDialog);

        void onRightClick(AlertDialog alertDialog);
    }
}






















